package examples.mail;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.lang.StringUtils;

public class MainTestUser {

	BlockingQueue<String> bq = new LinkedBlockingQueue<String>();
	Random rand = new Random();

	Map<String, List<String>> m = new HashMap<String, List<String>>();

	QueryRunner qr = Db.getQueryRunner();

	class SendThread extends Thread {
		ExecutorService executor = Executors.newSingleThreadExecutor();

		public void run() {
			
			
			while (true) {
				FutureTask<String> futureTask =null;
				String to1 = null;
				try {

					final String to = bq.take();
					to1 = to;

					Pattern regex = Pattern.compile("[a-zA-Z0-9_-]+@\\w+\\.[a-z]+(\\.[a-z]+)?");
					Matcher matcher = regex.matcher(to);
					if (!matcher.matches()) {
						qr.update("update acc_csdn set stat='mail.add.error'  where too=? ", to);
						continue;
					}

					String hostName = to.split("@")[1];

					System.out.println(to);
					if (hostName.equals("qq.com")) {
						// qr.update("update acc_csdn set stat='qq'  where too=? ",
						// to);
						continue;
					}

					List<String> mxs = null;

					if (m.containsKey(hostName)) {
						mxs = m.get(hostName);

					} else {
						mxs = DnsResolver.getMXRecordsForHost(hostName);
						m.put(hostName, mxs);
					}

					if (mxs == null) {
						qr.update("update acc_csdn set stat='nomx'  where too=? ", to);
						continue;
					}
					int r = Math.abs(rand.nextInt());

					final String toserver = mxs.get(r % mxs.size());

					final String mailserver = "qq.com";

					final String from = Util.randomString(10) + "@qq.com";

				 futureTask = new FutureTask<String>(new Callable<String>() {
						public String call() throws Exception {

							SMTPUtil.testUser(from, to, toserver, mailserver);

							return "";
						}
					});

					executor.execute(futureTask);
					
					// ���������������κ�����
					String result = futureTask.get(10000, TimeUnit.MILLISECONDS); // ȡ�ý��ͬʱ���ó�ʱִ��ʱ��Ϊ5�롣ͬ�������future.get()��������ִ�г�ʱʱ��ȡ�ý��

					futureTask.cancel(true);

					
					
					qr.update("update acc_csdn set stat='1'  where too=? ", to);

				} catch(TimeoutException e){
					executor = Executors.newSingleThreadExecutor();
					
					
					String msg = e.getMessage();
					if(msg==null){
						msg=e.getClass().getName();
					}
					
					
					if (msg != null && msg.length() > 150) {

						msg = msg.substring(0, 150);
					}

					try {
						qr.update("update acc_csdn set stat=?  where too=? ", msg, to1);
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
				}catch (Exception e) {
					if(futureTask!=null){
						futureTask.cancel(true);
					}
					
					e.printStackTrace();
					try {
						String msg = e.getMessage();
						if(msg==null){
							msg=e.getClass().getName();
						}
						
						
						if (msg != null && msg.length() > 150) {

							msg = msg.substring(0, 150);
						}

						qr.update("update acc_csdn set stat=?  where too=? ", msg, to1);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}

				/*
				 * try { Thread.sleep(500); } catch (InterruptedException e) {
				 * e.printStackTrace(); }
				 */

			}
		}
	}

	public void start() throws Exception {
		String sql = "select too from acc_csdn where stat='0'  limit 3000000     ";

		// sql=" select * from acc_csdn where  too='yangelhun@126.com' ";

		
		List<Object> tos = qr.query(sql, new ColumnListHandler("too"));
		for (Object to1 : tos) {
			String to = (String) to1;
			bq.add(to);
		}

		// bq.clear();
		// bq.add("yangelhun@126.com");

		for (int i = 0; i < 8; i++) {
			new SendThread().start();
		}

	}

	public static void main(String[] args) throws Exception {
		new MainTestUser().start();
	}
}
